package com.foreknow.tijian.controller;

import com.foreknow.tijian.po.Cart;
import com.foreknow.tijian.service.CartService;
import com.foreknow.tijian.service.Impl.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.List;

public class CartController {
    /**
     * http://localhost:8080/elm/CartController/saveCart
     * @param request
     * @return
     * @throws SQLException
     */
    public Object saveCart(HttpServletRequest request) throws SQLException {
        //获取前台的请求参数
        Cart cart=new Cart();
        cart.setFoodId(Integer.parseInt(request.getParameter("foodId")));
        cart.setBusinessId(Integer.parseInt(request.getParameter("businessId")));
        cart.setUserId(request.getParameter("userId"));
        //调用Service中的方法
       CartService cartService = new CartServiceImpl();
        int res=cartService.saveCart(cart.getUserId(), cart.getBusinessId(),cart.getFoodId());
        return res;
    }


    /**
     * 根据条件查询购物车列表
     * http://localhost:8080/elm/CartController/listCart?userId=8080&businessId=10001
     * @param request
     * @return
     */
    public Object listCart(HttpServletRequest request) {
        CartService service = new CartServiceImpl();
        Cart cart = new Cart();
        cart.setUserId(request.getParameter("userId"));
        cart.setBusinessId(Integer.parseInt(request.getParameter("businessId")));
        List<Cart> list = service.listCart(cart);
        return list;
    }




    /**
     * http://localhost:8080/elm/CartController/updateCart
     * @param request
     * @return
     * @throws SQLException
     */

    public Object updateCart(HttpServletRequest request) throws SQLException {
        //获取前台的请求参数
        Cart cart=new Cart();
        cart.setFoodId(Integer.parseInt(request.getParameter("foodId")));
        cart.setBusinessId(Integer.parseInt(request.getParameter("businessId")));
        cart.setUserId(request.getParameter("userId"));
        cart.setQuantity(Integer.parseInt(request.getParameter("quantity")));
        //调用Service中的方法
        CartService cartService = new CartServiceImpl();
        int res=cartService.updateCart(cart);
        return res;
    }
    /**
     * http://localhost:8080/elm/CartController/removeCart
     * @param request
     * @return
     * @throws SQLException
     */
    public Object removeCart(HttpServletRequest request) throws SQLException {
        //获取前台的请求参数
        Cart cart=new Cart();
        cart.setUserId(request.getParameter("userId"));
        cart.setBusinessId(Integer.parseInt(request.getParameter("businessId")));
        cart.setFoodId(Integer.parseInt(request.getParameter("foodId")));
        //调用Service中的方法
        CartService cartService = new CartServiceImpl();
        int res=cartService.removeCart(cart);
        return res;
    }
}
